$(function(){
    $("#submit").click(function(){
        $("#api-form").ajaxSubmit({
            success: function(response){
                var json =JSON.stringify(response, null, 4);
                var cont_return = $("#cont-return").children('.return-box').children('.title');
                if($('#pre'))$('#pre').remove();
                cont_return.after("<pre id='pre'>"+json+"</pre>");
            }
        });
    });

    //读取文件内容
    $.ajaxSettings.async = false;
    $.getJSON("/resource/js/configure.json",function(response){
        $.each(response.data, function(i,n){
            var title = n.title;
            $('#list').append('<li><a href="javascript:;" class="api" rel="'+i+'">'+title+'</a></li> ');
            $('[rel='+i+']').data(n);
        })
    });
    $(document).delegate('.api','click',function(){
        var data = $(this).data();
        $(this).addClass('active').parent().siblings().children().removeClass('active');
        $('#title').text(data.title+'_test');
        $('#method').text(data.method);
        $('#url').text(data.url);
        $('#api-form').attr('action',data.url);
        $('#api-form').attr('method',data.method);
        var fields = '';
        $.each(data.params, function(i,n){
            if(n.input_type == 'text'){
                fields += "<div class=\"form-group\">"+
                "<label class=\"left control-label\">"+n.input_title+"</label>"+
                "<div class=\"left col-md-9 col-sm-9 col-xs-12\">"+
                "<input  name=\""+n.input_name+"\" type=\"text\" class=\"form-control\" value=\""+n.default_value+"\" placeholder=\"\">"+
                "</div>"+
                "</div>";
            }
            $('#api-form').empty().html(fields);
        });
    });
    $(".api").eq(0).trigger("click");

    //下拉
    var left_title = $("#left-list").children('.title');
    left_title.click(function(){
        var nc_type = $(this).attr('nc_type');
        if(nc_type == 'block'){
            $(this).next('ul').hide();
            $(this).attr('nc_type','none');
        }
        if(nc_type == 'none'){
            $(this).next('ul').show();
            $(this).attr('nc_type','block');
        }
    });
});